package com.xinghanlaser.applet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinghanlaser.applet.base.common.entity.ResponseEntity;
import com.xinghanlaser.applet.base.common.exception.ServiceException;
import com.xinghanlaser.applet.base.common.utils.QueryUtil;
import com.xinghanlaser.applet.entity.KnowledgeBase;
import com.xinghanlaser.applet.query.KnowledgeQuery;
import com.xinghanlaser.applet.service.IKnowledgeBaseService;
import com.xinghanlaser.applet.mapper.KnowledgeBaseMapper;
import com.xinghanlaser.applet.vo.applet.KnowledgePostVO;
import com.xinghanlaser.applet.vo.applet.KnowledgePutVO;
import com.xinghanlaser.applet.vo.applet.KnowledgeVO;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author XH-YF-TSDN-0080
* @description 针对表【knowledge_base】的数据库操作Service实现
* @createDate 2024-05-08 15:21:59
*/
@Service
public class KnowledgeBaseServiceImpl extends ServiceImpl<KnowledgeBaseMapper, KnowledgeBase>
    implements IKnowledgeBaseService {

    @Override
    public ResponseEntity<ResponseEntity.PageResult<KnowledgeVO>> pageByCondition(KnowledgeQuery query) {
        ResponseEntity.PageResult<KnowledgeVO> result = QueryUtil.query(
                query,
                () -> this.list(Wrappers.lambdaQuery(new KnowledgeBase())
                        .eq(StrUtil.isNotBlank(query.getUserName()), KnowledgeBase::getCreateUser, query.getUserName())
                        .eq(query.getType() != null, KnowledgeBase::getType, query.getType())
                ),

                doList -> doList.stream()
                        .map(item -> BeanUtil.copyProperties(item, KnowledgeVO.class))
                        .collect(Collectors.toList())
        );

        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity<KnowledgeVO> getKnowledgeById(Long id) {
        KnowledgeBase entity = validatedKnowledgeId(id);
        entity.setViewVolume(entity.getViewVolume() + 1);

        this.updateById(entity);

        KnowledgeVO vo = BeanUtil.copyProperties(entity, KnowledgeVO.class);
        return ResponseEntity.ok(vo);
    }

    @Override
    public ResponseEntity<Void> addKnowledge(KnowledgePostVO postVO) {
        KnowledgeBase entity = BeanUtil.copyProperties(postVO, KnowledgeBase.class);

        this.save(entity);

        return ResponseEntity.okMsg("添加成功");
    }

    @Override
    public ResponseEntity<Void> modKnowledge(KnowledgePutVO putVO) {
        validatedKnowledgeId(putVO.getId());

        KnowledgeBase entity = BeanUtil.copyProperties(putVO, KnowledgeBase.class);

        this.updateById(entity);

        return ResponseEntity.okMsg("修改成功");
    }

    private KnowledgeBase validatedKnowledgeId(Long id){
        return Optional.ofNullable(this.getById(id))
                .orElseThrow(() -> new ServiceException("知识不存在"));
    }
}




